package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import module.Unit;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import utils.PageUtil;
import bussinessService.basicbussinessService;
import bussinessService.impl.UnitBussinessServiceImpl;
import domain.vo.JSONObject;
import domain.vo.Message;

import domain.vo.UnitQueryObject;
import domain.vo.UnitVO;

/**
 * 
* @ClassName: UnitController 
* @Description:小区MVC
* @author chenchao
* @date 2014年1月17日 上午10:31:04 
*
 */
@Controller
@RequestMapping("/unit")
public class UnitController {
	private Logger logger = Logger.getLogger(UnitController.class.getName());
	private basicbussinessService<Unit, UnitVO> unitBIS = new UnitBussinessServiceImpl();
	
	@RequestMapping("/getAllUnitJson/{districtId}")
	public @ResponseBody JSONObject<UnitVO> getAllShopJson(@PathVariable("districtId") int districtId) {
		JSONObject<UnitVO> jsonObject = new JSONObject<UnitVO>();
		UnitQueryObject uqo = new UnitQueryObject();
		uqo.setDistrictId(districtId);
		uqo.setStart(-1);
		try {
			List<UnitVO> tempVOS = new ArrayList<UnitVO>(); 
			tempVOS = unitBIS.getObjectByQueryObject(uqo);
			
			UnitVO tvo = new UnitVO();
			tvo.setUnitId(-1);
			tvo.setBlockId(-1);
			tvo.setBlockName("未知");
			tvo.setDistrictId(-1);
			tvo.setDistrictName("未知");
			tvo.setUnitName("未知");
			tvo.setSelected(true);
			tempVOS.add(0, tvo);
			
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	
	@RequestMapping("/getAllJsonByBlockId/{blockId}")
	public @ResponseBody JSONObject<UnitVO> getAllJsonByBlockId(@PathVariable("blockId") int blockId) {
		JSONObject<UnitVO> jsonObject = new JSONObject<UnitVO>();
		UnitQueryObject uqo = new UnitQueryObject();
		uqo.setBlockId(blockId);
		try {
			List<UnitVO> tempVOS = new ArrayList<UnitVO>(); 
			tempVOS = unitBIS.getObjectByQueryObject(uqo);
			UnitVO tvo = new UnitVO();
			tvo.setUnitId(-1);
			tvo.setUnitName("小区不限");
			tempVOS.add(0,tvo);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/getAllUJsonByBlockId/{blockId}")
	public @ResponseBody JSONObject<UnitVO> getAllShopJsonByBlockId(@RequestParam("page") int page, @RequestParam("rows") int rows,@PathVariable("blockId") int blockId) {
		JSONObject<UnitVO> jsonObject = new JSONObject<UnitVO>();
		UnitQueryObject uqo = new UnitQueryObject();
		PageUtil.Page(page, rows);
		uqo.setStart(PageUtil.start);
		uqo.setEnd(PageUtil.end);
		uqo.setBlockId(blockId);
		try {
			List<UnitVO> tempVOS = new ArrayList<UnitVO>(); 
			tempVOS = unitBIS.getObjectByQueryObject(uqo);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/addUnit")
	public @ResponseBody  Message saveUnit(@RequestParam("bhaid2") int bhaid,@RequestParam("blockid2") int blockId,Unit unit ) {
		Message ms = new Message();
		try {
			unit.setBlockId(blockId);
			unit.setDistrictId(bhaid);
			unitBIS.merageObject(unit);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
	
	@RequestMapping("/modifyUnit")
	public @ResponseBody  Message modifyUnit(@RequestParam("bhaid2") int bhaid,@RequestParam("blockid2") int blockId,Unit unit ) {
		Message ms = new Message();
		try {
			unit.setBlockId(blockId);
			unit.setDistrictId(bhaid);
			unitBIS.merageObject(unit);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
}
